package com.energyfuture.system.controller;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.energyfuture.common.core.constant.UserConstants;
import com.energyfuture.common.core.utils.RegexUtil;
import com.energyfuture.common.core.utils.StringUtils;
import com.energyfuture.common.core.web.controller.BaseController;
import com.energyfuture.common.core.web.domain.AjaxResult;
import com.energyfuture.common.core.web.page.TableDataInfo;
import com.energyfuture.common.log.annotation.Log;
import com.energyfuture.common.log.enums.BusinessType;
import com.energyfuture.common.security.annotation.PreAuthorize;
import com.energyfuture.system.api.domain.SysCompany;
import com.energyfuture.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 用户信息
 *
 * @author energyfuture
 */
@RestController
@RequestMapping("/company")
public class SysCompanyController extends BaseController {


    @Autowired
    private ISysCompanyService companyService;


    /**
     * 获取单位列表
     */
    @PreAuthorize(hasRole="ROLE_admin")
    @GetMapping("/list")
    public TableDataInfo list(SysCompany company) {
        startPage();
        List<SysCompany> list = companyService.selectList(company);
        return getDataTable(list);
    }

    
    /**
     * 根据单位编号获取详细信息
     */
    @PreAuthorize(hasRole="ROLE_admin")
    @GetMapping(value = {"/", "/{companyId}"})
    public AjaxResult getInfo(@PathVariable(value = "companyId", required = false) String companyId) {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(companyId)) {
            ajax.put(AjaxResult.DATA_TAG, companyService.getById(companyId));

        }
        return ajax;
    }

    /**
     * 新增单位
     */
    @PreAuthorize(hasRole="ROLE_admin")
    @Log(title = "新增单位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody  SysCompany company) {
        if(company==null||company.getCompanyname()==null||company.getCompanyname().equals("")||company.getCompanyname().equals("null")){
            return AjaxResult.error("新增单位失败，单位名称为空");
        }
        if (UserConstants.NOT_UNIQUE.equals(companyService.checkCompanyNameUnique(company.getCompanyname()))) {
            return AjaxResult.error("新增单位'" + company.getCompanyname() + "'失败，新增单位已存在");
        }
        //正则表达式检查
        if (!company.getPhone().matches(RegexUtil.PH_PATTERN)) {
        	return AjaxResult.error("手机号不正确");
        }
        company.setId(UuidUtils.generateUuid());
        company.setStatus(0);
        company.setUpdatetime(new Date());
        return toAjax(companyService.save(company));
    }

    /**
     * 修改单位信息
     */
    @PreAuthorize(hasRole="ROLE_admin")
    @Log(title = "修改单位信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SysCompany company) {
        company.setStatus(0);
        company.setUpdatetime(new Date());
        return toAjax(companyService.updateById(company));
    }

    /**
     * 删除单位
     */
    @PreAuthorize(hasRole="ROLE_admin")
    @Log(title = "删除单位", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(@PathVariable String companyId) {
        SysCompany company= companyService.getById(companyId);
        company.setStatus(1);
        company.setUpdatetime(new Date());
        return toAjax(companyService.updateById(company));
    }
}
